package cn.edu.zknu.mita.controller;

import cn.edu.zknu.mita.model.FindImage;
import cn.edu.zknu.mita.model.FindMsg;
import cn.edu.zknu.mita.model.User;
import cn.edu.zknu.mita.service.IFindMsgService;
import cn.edu.zknu.mita.service.IFindWallService;
import cn.edu.zknu.mita.util.UploadUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

/**
 * Created by iwlnner on 2020/3/22.
 */
@Controller
public class ReleaseController {

    @Autowired
    private UploadUtils uploadUtils;
    @Autowired
    private IFindMsgService findMsgService;
    @Autowired
    private IFindWallService findWallService;

    @RequestMapping(value = "/release",method = RequestMethod.POST)
    public String releaseGoods(FindMsg msg,HttpSession session){
        User user = (User) session.getAttribute("USER_IN_SESSION");
        if(user!=null) {
            msg.setUserId(user.getId());
        }

        Long id = findMsgService.release(msg);
        //解析detail中的html
        return "redirect:/detail?id="+id;
    }


    @ResponseBody
    @RequestMapping(value = "/upload")
    public String upload(String dir,MultipartFile imgFile,HttpSession session){
        String ret = uploadUtils.uploadImage(dir, imgFile);
        //System.out.println(ret);
        return ret;
        /*ObjectMapper obj=new ObjectMapper();
        try {
            Map resultMap = obj.readValue(result, Map.class);
            Integer error=(Integer)resultMap.get("error");
            if(error==0){
                String url=(String)resultMap.get("url");
                User user = (User) session.getAttribute("user");
                //查询该userID的findMsgId为0的fimg是否存在
                //如果不存在
                findWallService.saveImg(new FindImage(url, user.getId()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        //return "{\"error\" : 0,\"url\" : \"/img/011.jpg\"}";
    }

    @ResponseBody
    @RequestMapping(value = "/checkRandom")
    public String checkRandom(String random,HttpSession session){
        if(!random.toLowerCase().equals(session.getAttribute("RANDOMCODE_IN_SESSION").toString().toLowerCase())){
            return "false";
        }
        return "true";
    }

    @RequestMapping("/releaseInformation")
    public String releaseInformation(String key, HttpSession session, Model model){
        if(session.getAttribute("USER_IN_SESSION")==null){
            model.addAttribute("key",key);
            return "login";
        }
        if("lostor".equals(key)){
            model.addAttribute("key","lostor");
        }else {
            model.addAttribute("key", "ta");
        }
        return "ReleaseInformation";
    }
}
